// 坦克基础子弹
class TankDefaultBullet {
    constructor(
        id,
        tank_id,
        bullet_level,
        ctx,
        camp,
        bullet_init_point = [0, 0],
        bullet_target_point = [0, 0],
    ) {
        // 子弹信息，用于绑定对应坦克
        this.id = id
        this.fire_tank_id = tank_id
        // 阵营
        this.camp = camp
        this.ctx = ctx
        // 火力等级，决定子弹伤害，子弹速度，特殊子弹的效果
        this.bullet_level = bullet_level
        // 目标坐标，但不是子弹终点
        this.target_point = bullet_target_point
        // 子弹起始位置
        this.bullet_init_point = bullet_init_point
        // 子弹旋转角度
        this.bullet_rorate = 0
        // 子弹射程
        this.bullet_range = 500 + (50 * (bullet_level - 1))
        this.bullet_size = [10, 4]
        // 子弹移动的x,y的移动量
        this.bullet_move = [0, 0]
        // 已经移动的距离，用于计算移动是否到上限
        this.move_mileage = 0
        // 是否达到子弹移动上限
        this.mileage_flag = false
        // 子弹是否和坦克发生碰撞
        this.bullet_and_tank_crash = false
        // 碰撞到的坦克id
        this.bullet_crash_tank_id = []
        // 是否已执行消失特效
        this.hide_special_effect = false
        this.bulletInit()
    }

    // 子弹初始化
    bulletInit() {
        const bullet_atan_y = this.target_point[1] - this.bullet_init_point[1]
        const bullet_atan_x = this.target_point[0] - this.bullet_init_point[0]
        this.bullet_rorate = Math.atan2(bullet_atan_y, bullet_atan_x)
        // 计算子弹每次移动的距离
        const x_range1 = Math.cos(this.bullet_rorate) * (5 + this.bullet_level)
        const y_range1 = Math.sin(this.bullet_rorate) * (5 + this.bullet_level)
        this.bullet_move[0] = Number(x_range1.toFixed(2))
        this.bullet_move[1] = Number(y_range1.toFixed(2))
        this.drawBullet()
    }

    // 子弹运行轨迹
    movingTrajectory() {
        this.bullet_init_point[0] += this.bullet_move[0]
        this.bullet_init_point[1] += this.bullet_move[1]
        this.move_mileage += Number(Math.sqrt(Math.pow(this.bullet_move[0], 2) + Math.pow(this.bullet_move[1], 2)).toFixed(2))
        if (this.move_mileage >= this.bullet_range) {
            this.mileage_flag = true
        }
        this.drawBullet()
        this.checkDeleteBullet()
    }

    // 子弹绘制
    drawBullet() {
        this.ctx.save()
        this.ctx.fillStyle = '#232e7c'
        const bullet_x_point = this.bullet_init_point[0]
        const bullet_y_point = this.bullet_init_point[1]
        this.ctx.translate(bullet_x_point, bullet_y_point)
        this.ctx.rotate(this.bullet_rorate)
        this.ctx.beginPath()
        this.ctx.moveTo(-this.bullet_size[0] / 2, -this.bullet_size[1] / 2)
        this.ctx.lineTo(this.bullet_size[0] / 2, -this.bullet_size[1] / 2)
        this.ctx.lineTo(this.bullet_size[0] / 2, this.bullet_size[1] / 2)
        this.ctx.lineTo(-this.bullet_size[0] / 2, this.bullet_size[1] / 2)
        this.ctx.closePath()
        this.ctx.fill()
        this.ctx.restore()
        this.bulletCrashCheck()
    }

    // 子弹碰撞检查
    bulletCrashCheck() {
        // 碰撞检查
        const check_tank_list = this.camp === 1 ? computer_players_tank : players_tank
        const checkPintAndPolygon = new PointAndPolygonCrash()
        check_tank_list.forEach((tank) => {
            checkPintAndPolygon.setCrashGraphics(this.bullet_init_point, tank.tank_polygon_point)
            const crash_flag = checkPintAndPolygon.getCrashCheck()
            if (crash_flag) {
                this.bullet_and_tank_crash = true
                tank.deleteTankSelf()
            }
        })
    }

    // 检查是否需要移除子弹, 可以继承, 执行一些特殊操作
    checkDeleteBullet() {
        if (this.mileage_flag || this.bullet_and_tank_crash) {
            this.deleteAllBullet()
        }
    }

    // 删除在坦克实例和全局子弹列表中的子弹对象
    deleteAllBullet() {
        const tank = all_tank.get(this.fire_tank_id)
        tank.deleteBullet()
        all_bullet.delete(this.id)
        this.bulletHideAfterSpecialEffects()
    }
    // 子弹在消失后执行特效, 可以继承
    bulletHideAfterSpecialEffects() {
        const effect = new NormalSpecialEffect(this.id, this.bullet_level, this.bullet_init_point, 10, this.ctx)
        special_effects_list.set(this.id, effect)
    }
}


